package com.ktjiaoyu.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class SysUser implements Serializable {
    private Long id;
    private String account;
    private String realName;
    private String password;
    private Integer sex;

    private String phone;
    private String address;
    private String idPicPath;
    private String workPicPath;
    private Long roleId;
    private Long createdUserId;
    private Date createdTime;
    private Long updatedUserId;
    private Date updatedTime;
    private SysRole role;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday;

    public Integer getAge(){
        if (this.birthday==null){
            return 0;
        }
        //根据生日计算年龄使用 LocalDate和Period实现
        LocalDate birthLocalDate = this.birthday.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); // 将Date转换为LocalDate 获取年月日
        LocalDate currentDate = LocalDate.now();// 获取当前日期
        return Period.between(birthLocalDate, currentDate).getYears(); // 计算年龄
    }
}
